<html>
<head><meta charset="utf-8"><title>Interaction with const evaluation · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html">Interaction with const evaluation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="190651637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190651637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190651637">(Mar 15 2020 at 15:19)</a>:</h4>
<p>Is the following code valid?</p>
<div class="codehilite"><pre><span></span><span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">read_from_port</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">port</span>: <span class="kt">u16</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">port</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="mi">255</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">asm</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;in al, {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">port</span><span class="p">,</span><span class="w"> </span><span class="n">out</span><span class="p">(</span><span class="s">&quot;al&quot;</span><span class="p">)</span><span class="w"> </span><span class="n">value</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">asm</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;in al, dx&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">in</span><span class="p">(</span><span class="s">&quot;dx&quot;</span><span class="p">)</span><span class="w"> </span><span class="n">port</span><span class="p">,</span><span class="w"> </span><span class="n">out</span><span class="p">(</span><span class="s">&quot;al&quot;</span><span class="p">)</span><span class="w"> </span><span class="n">value</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">value</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="190651697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190651697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190651697">(Mar 15 2020 at 15:21)</a>:</h4>
<p>The issue here is that the <code>in</code> instruction only accepts immediates in the range 0 - 255. However because of the way we currently perform codegen, the invalid asm (first branch of the <code>if</code>) will never reach LLVM.</p>



<a name="190651701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190651701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190651701">(Mar 15 2020 at 15:21)</a>:</h4>
<p>(Assuming that port &gt; 255)</p>



<a name="190651753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190651753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190651753">(Mar 15 2020 at 15:22)</a>:</h4>
<p>I don't believe dead code elimination is guaranteed. At least with <code>-Zmir-opt-level=0</code> it shouldn't be performed.</p>



<a name="190651811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190651811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190651811">(Mar 15 2020 at 15:23)</a>:</h4>
<p>Even then, LLVM may still eliminate it before it reaches the assembler.</p>



<a name="190651830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190651830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190651830">(Mar 15 2020 at 15:23)</a>:</h4>
<p>Hmm, actually I know a way of fixing this particular example: just use <code>port &amp; 255</code>.</p>



<a name="190806841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190806841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190806841">(Mar 17 2020 at 04:12)</a>:</h4>
<p><span class="user-mention" data-user-id="143274">@Amanieu</span> Or <code>port as u8</code>.</p>



<a name="190816891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Interaction%20with%20const%20evaluation/near/190816891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Interaction.20with.20const.20evaluation.html#190816891">(Mar 17 2020 at 08:10)</a>:</h4>
<p>In 1.43.0, you'll be able to write <code>port &amp; u8::MAX</code> with no imports, which helps avoid magic numbers and <code>as</code>. <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>